package org.jeecg.modules.htxincailiao.cart.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.htxincailiao.cart.domain.vo.HTCartUpdateVO;
import org.jeecg.modules.htxincailiao.goods.service.IHTGoodsService;
import org.jeecg.modules.htxincailiao.cart.domain.vo.HTCartVO;
import org.jeecg.modules.htxincailiao.cart.entity.HTCart;
import org.jeecg.modules.htxincailiao.cart.mapper.HTCartMapper;
import org.jeecg.modules.htxincailiao.order.constant.HTOrderTypeEnum;
import org.jeecg.modules.htxincailiao.shop.mapper.HTShopMapper;
import org.jeecg.modules.htxincailiao.cart.service.IHTCartService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.jeecg.common.util.oConvertUtils;

import static org.jeecg.modules.htxincailiao.order.constant.HTOrderTypeEnum.*;

/**
 * @Description: ht_cart
 * @Author: jeecg-boot
 * @Date:   2021-06-07
 * @Version: V1.0
 */
@Service
public class HTCartServiceImpl extends ServiceImpl<HTCartMapper, HTCart> implements IHTCartService {

    @Override
    public HTCart getByGoodsId(String goodsId) {
        return baseMapper.getByGoodsId(goodsId );
    }

    @Override
    public HTCart getByLibraryId(String libraryId) {
        return baseMapper.getByLibraryId(libraryId);
    }

    @Override
    public HTCart getByReportId(String reportId) {
        return baseMapper.getByReportId(reportId);
    }

    @Override
    public HTCartUpdateVO updateBatch(List<HTCartVO> cartList) {
        HTCartUpdateVO updateVO = new HTCartUpdateVO();
        updateVO.setSucceed(false);

        ArrayList<String> goodsList = new ArrayList<String>();
        ArrayList<String> libraryList = new ArrayList<String>();

        for (HTCartVO cartVO :cartList) {
            HTCart cart = new HTCart();
            BeanUtil.copyProperties(cartVO, cart);
            cart.setId(cartVO.getCartId());
            updateVO.setSucceed(updateById(cart));
            if (oConvertUtils.isNotEmpty(cartVO.getGoodsId())) {
                goodsList.add(cartVO.getGoodsId());
            }
            if (oConvertUtils.isNotEmpty(cartVO.getLibraryId())){
                libraryList.add(cartVO.getLibraryId());
            }
        }
        if (goodsList.size()>0 && libraryList.size()>0){
            updateVO.setOrderType(MUTABLE.getValue());
        }
        else if(goodsList.size()>0 && libraryList.size()==0){
            updateVO.setOrderType(GOODS.getValue());
        }
        else if(goodsList.size()==0 && libraryList.size()>0){
            updateVO.setOrderType(LIBRARY.getValue());
        }

        return updateVO;
    }

    @Override
    public HTOrderTypeEnum getOrderType() {
        long pageNo = 1;
        long pageSize = Integer.MAX_VALUE;
        Page<HTCart> page = new Page<HTCart>(pageNo, pageSize);
        IPage<HTCart> pageList = page(page);
        List<HTCart> cartList = pageList.getRecords();
        HTOrderTypeEnum orderTypeEnum = null;
        ArrayList<String> goodsList = new ArrayList<String>();
        ArrayList<String> libraryList = new ArrayList<String>();
        ArrayList<String> reportList = new ArrayList<String>();
        for (HTCart cart :cartList) {
            if (cart.getChecked() && oConvertUtils.isNotEmpty(cart.getGoodsId())) {
                goodsList.add(cart.getGoodsId());
            }
            if (cart.getChecked() && oConvertUtils.isNotEmpty(cart.getLibraryId())){
                libraryList.add(cart.getLibraryId());
            }
            if (cart.getChecked() && oConvertUtils.isNotEmpty(cart.getReportId())){
                reportList.add(cart.getReportId());
            }
        }
        if (goodsList.size()>0 && libraryList.size()>0 && reportList.size()>0){
            orderTypeEnum = MUTABLE;
        }
        else if(goodsList.size()>0){
            orderTypeEnum = GOODS;
        }
        else if(libraryList.size()>0){
            orderTypeEnum = LIBRARY;
        }
        else if(reportList.size()>0){
            orderTypeEnum = REPORT;
        }
        return orderTypeEnum;
    }
}
